﻿using System;
using System.ComponentModel;
using System.IO;
using System.Windows.Forms;
using ApplicationGenerator.DataAccess;
using WeifenLuo.WinFormsUI.Docking;


namespace GUserInterface
{

    #region Delegates ...

    public delegate void WorkflowSaved(string title);

    #endregion

    public partial class WorkflowPanel : DockContent
    {
        #region Variables

        public WorkflowDesignerControl wdc = new WorkflowDesignerControl();
        
        #endregion

        #region Events
        
        public event UpdatePropertyGridHandler UpdatePropertyGridEvent;
        public event WorkflowSaved WorkflowSavedEvent;
        public event RefreshHandler RefreshEvent;

        /*
        public event UpdatePropertyGridHandler UpdatePropertyGridEvent;
        
        public event ValidationFailedHandler ValidationFailedEvent;
        public event FormUpdated FormUpdatedEvent;*/

        #endregion

        #region Properties

        [Category("Dane podstawowe")]
        [DisplayName("Tytul Workflow")]
        [Description("Tytul Workflow w Designerze")]
        public String Title
        {
            get { return this.TabText; }
            set
            {
                string oldText = this.TabText;
                this.TabText = value;
                RefreshEvent(oldText);
            }
        }

        #endregion


        #region Construction

        public WorkflowPanel()
        {
            InitializeComponent();
            InitializeComponent_Custom();
        }

        public WorkflowPanel( string xoml )
        {
            //wiem ze brzydkie rozwiazanie, ale nie mamy teraz na nic innego czasu ...
            InitializeComponent();
            StreamWriter sw = new StreamWriter("tmp.xoml");
            sw.Write(xoml);
            sw.Close();
            wdc.Dock = DockStyle.Fill;
            wdc.LoadWorkflow(@"tmp.xoml");
            InitializeComponent_Custom();
        }

        private void InitializeComponent_Custom()
        {
            this.AllowDrop = true;
            wdc.Dock = DockStyle.Fill;
            wdc.UpdatePropertyGridEvent += new UpdatePropertyGridHandler(updatePropertyGrid_handler);
            this.panel1.Controls.Add(wdc);
        }

        #endregion

        private void updatePropertyGrid_handler(object toUpdate)
        {
            UpdatePropertyGridEvent(toUpdate);
        }

        public WorkflowDesignerControl WDC
        {
            get { return wdc; }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //save and update compile units
            string fileName = this.TabText + ".xoml";
            wdc.Save(fileName);
            StreamReader sr = new StreamReader(fileName);
            string xomlFileContent = sr.ReadToEnd();


            Guid g = Guid.NewGuid();
            //try to save or update
            //if (false == BusinessModule.Utilities.FormSerializer.SaveFormControlContainter(fcc, g, this.Title))
            
            System.Text.ASCIIEncoding  encoding = new System.Text.ASCIIEncoding();
            byte[] bytes = encoding.GetBytes(xomlFileContent);

            if (false == Serializer.Serialize(Policy.Workflow, bytes, g, this.Title))
            {
                Serializer.Update(Policy.Workflow, bytes, this.Title);
            }
            else
            {
                WorkflowSavedEvent(this.TabText);
            }

            this.Close();
            
            
            /*SaveFileDialog saveFileDialog = new SaveFileDialog();

            saveFileDialog.Filter = "xoml files (*.xoml)|*.xoml|All files (*.*)|*.*";
            saveFileDialog.FilterIndex = 1;
            saveFileDialog.RestoreDirectory = true;
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                this.wdc.Save(saveFileDialog.FileName);
            }*/


        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            
            wdc.CompileWorkflow(this.TabText);
        }

    }
}
